
import base64
import json

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad


class EncryptUtil:

    @staticmethod
    def encrypt_aes(key, data):
        # 创建AES对象
        cipher = AES.new(key, AES.MODE_ECB)
        # 填充数据
        padded_data = pad(data.encode(), AES.block_size)
        # 加密数据
        encrypted_bytes = cipher.encrypt(padded_data)
        # 返回base64编码的加密字符串
        return base64.b64encode(encrypted_bytes).decode()

    @staticmethod
    def decrypt_aes(key, encrypted_data):
        # 创建AES对象
        cipher = AES.new(key, AES.MODE_ECB)
        # 解码base64编码的加密字符串
        encrypted_bytes = base64.b64decode(encrypted_data)
        # 解密数据
        decrypted_padded_data = cipher.decrypt(encrypted_bytes)
        # 去除填充
        decrypted_data = unpad(decrypted_padded_data, AES.block_size)
        return decrypted_data.decode()

if __name__ == '__main__':
    key = b"pinghang@1232025"  # 16字节密钥
    data = "Hello, World!"

    encrypted_data = EncryptUtil.encrypt_aes(key, data)
    print("Encrypted:", encrypted_data)

    decrypted_data = EncryptUtil.decrypt_aes(key, encrypted_data)
    print("Decrypted:", decrypted_data)

#    self.host = config.get('host')
    #         self.port = int(config.get('port', 21))
    #         self.username = config.get('username')
    #         self.password = config.get('password')
    #         self.pasv_mode = config.get('pasvMode', True)  # 默认为主动模式
    #         self.encoding = config.get('encoding', 'utf-8')  # 默认字符集为utf-8
    ftp_config = {
        'host': '10.118.21.239',
        'port': 53521,
        'username': 'ftpfile',
        'password': '**********',
        'pasvMode': False,
        'encoding': 'utf-8'
    }
    print(EncryptUtil.encrypt_aes(key, json.dumps(ftp_config)))
